<?php

class CommentController extends Zend_Controller_Action
{
	public function init()
    {
    	$session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
    	if (isset($session->user) == false) {
    		$this->_redirect("auth");
    	}
    }

    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {
    	$base = $this->_request->getBaseUrl();
        $this->view->base=$base;

        $session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
        $user = $session->user;
        $this->view->username = $user->username;
    }

    public function listAction ()
    {
        $response = $this->getResponse();
        $response->setHeader('Content-Type', 'text/html; charset=utf-8');

        $session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
        $condtion = $session->condition;

    	//Lấy danh sách user
        $comment = new Sc_Service_Comment();
        if (isset($condtion) == false) {
        	$data = $comment->listComment(null, null, null);
        } else {
        	$data = $comment->listComment($condtion->vn, $condtion->jp, $condtion->username);
        }
        

        //Lấy Base URL
        $this->view->base = $this->_request->getBaseUrl();

        //Đưa danh sách user vào Zend_Paginator để bắt đầu phân trang
        $paginator = Zend_Paginator::factory($data);

        //Số user trên một trang
        $paginator->setItemCountPerPage(12);

        //Số trang được hiện ra để click
        $paginator->setPageRange(3);

        //Lấy trang hiện tại
        $currentPage = $this->_request->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);

        //Truyền dữ liệu ra view
        $this->view->data=$paginator;

        //Không load layout
        $this->_helper->layout->disableLayout();
    }

    public function addAction()
    {
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		$session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
        	$user = $session->user;

    		$comment = new Sc_Comment();
    		$comment->vn = $request->getParam('vn', 'empty');
    		$comment->jp = $request->getParam('jp', 'empty');
    		$comment->userid = $user->id;

    		$service = new Sc_Service_Comment();
    		$result = $service->addComment($comment);

    		echo $result;
    		$this->_helper->viewRenderer->setNoRender();
    	}

    	$this->_helper->layout->disableLayout();
    }

	public function editAction()
    {
    	$view = $this->view;

    	$request = $this->getRequest();

    	if ($request->isPost()) {
    		$session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
        	$user = $session->user;

    		$comment = new Sc_Comment();
    		$comment->id = $request->getParam('id', 'empty');
    		$comment->vn = $request->getParam('vn', 'empty');
    		$comment->jp = $request->getParam('jp', 'empty');
    		$comment->userid = $user->id;

    		$service = new Sc_Service_Comment();
    		$result = $service->editComment($comment);

    		echo $result;
    		$this->_helper->viewRenderer->setNoRender();
    	} else {
	    	$id = $request->getParam("id", null);
	    	if (isset($id)) {
	    		$service = new Sc_Service_Comment();
	    		$comment = $service->getCommentById($id);

	    		if ($comment != false) {
	    			$view->id = $comment->id;
		    		$view->vn = $comment->vn;
		    		$view->jp = $comment->jp;
	    		}
	    	}
    	}

    	$this->_helper->layout->disableLayout();
    }

	public function deleteAction()
    {
    	$view = $this->view;

    	$request = $this->getRequest();

    	if ($request->isPost()) {
    		$session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
        	$user = $session->user;

    		$comment = new Sc_Comment();
    		$comment->id = $request->getParam('id', 'empty');
    		$comment->userid = $user->id;

    		$service = new Sc_Service_Comment();
    		$result = $service->deleteComment($comment);

	   		echo $result;
    		$this->_helper->viewRenderer->setNoRender();
    	} else {
	    	$id = $request->getParam("id", null);
	    	if (isset($id)) {
	    		$service = new Sc_Service_Comment();
	    		$comment = $service->getCommentById($id);

	    		if ($comment != false) {
	    			$view->id = $comment->id;
	    		}
	    	}
    	}

    	$this->_helper->layout->disableLayout();
    }

    public function searchAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
    	$this->_helper->layout->disableLayout();
    }

    public function saveconditionAction()
    {
        $session = new Zend_Session_Namespace(Sc_Util_Const::SC_NAMESPACE);
        $condtion = new Sc_ConditionSearch();

        $request = $this->getRequest();
        $condtion->vn = $request->getParam('vn', null);
		$condtion->jp = $request->getParam('jp', null);
		$condtion->username = $request->getParam('username', null);

        $session->condition = $condtion;

        $this->_helper->viewRenderer->setNoRender();
    	$this->_helper->layout->disableLayout();
    	var_dump($condtion);
    }
}
